2 * Copyright (c) 2013-2014 Apple Inc. All Rights Reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
21 * @APPLE_LICENSE_HEADER_END@
25 #import "KNPersistantState.h"
27 @implementation KNPersistantState
29 -(NSURL*)urlForStorage
31 return [NSURL URLWithString:@"Preferences/com.apple.security.KCN.plist" relativeToURL:[[NSFileManager defaultManager] URLForDirectory:NSLibraryDirectory inDomain:NSUserDomainMask appropriateForURL:nil create:YES error:nil]];
34 +(instancetype)loadFromStorage
36 KNPersistantState *state = [[KNPersistantState alloc] init];
41 id plist = @{@"lastWritten": [NSDate distantPast]};
44 NSData *stateData = [NSData dataWithContentsOfURL:[state urlForStorage] options:0 error:&error];
46 NSLog(@"Can't read state data (p=%@, err=%@)", [state urlForStorage], error);
48 NSPropertyListFormat format;
49 plist = [NSPropertyListSerialization propertyListWithData:stateData options: NSPropertyListMutableContainersAndLeaves format:&format error:&error];
52 NSLog(@"Can't deserialize %@, e=%@", stateData, error);
56 state.lastCircleStatus = plist[@"lastCircleStatus"] ? [plist[@"lastCircleStatus"] intValue] : kSOSCCCircleAbsent;
57 state.lastWritten = plist[@"lastWritten"];
58 state.pendingApplicationReminderInterval = plist[@"pendingApplicationReminderInterval"];
59 state.debugLeftReason = plist[@"debugLeftReason"];
60 state.pendingApplicationReminder = plist[@"pendingApplicationReminder"];
61 state.applcationDate = plist[@"applcationDate"];
62 if (!state.applcationDate) {
63 state.applcationDate = [NSDate distantPast];
65 if (!state.pendingApplicationReminder) {
66 state.pendingApplicationReminder = [NSDate distantFuture];
68 if (!state.pendingApplicationReminderInterval || [state.pendingApplicationReminderInterval doubleValue] <= 0) {
69 state.pendingApplicationReminderInterval = [NSNumber numberWithUnsignedInt:60 * 60 * 24 * 2];
77 NSMutableDictionary *plist = [@{@"lastCircleStatus": [NSNumber numberWithInt:self.lastCircleStatus],
78 @"lastWritten": [NSDate date],
79 @"applcationDate": self.applcationDate,
80 @"pendingApplicationReminder": self.pendingApplicationReminder,
82 if (self.debugLeftReason) {
83 plist[@"debugLeftReason"] = self.debugLeftReason;
85 if (self.pendingApplicationReminderInterval) {
86 plist[@"pendingApplicationReminderInterval"] = self.pendingApplicationReminderInterval;
88 NSLog(@"writeToStorage plist=%@", plist);
91 NSData *stateData = [NSPropertyListSerialization dataWithPropertyList:[plist copy] format:NSPropertyListXMLFormat_v1_0 options:kCFPropertyListImmutable error:&error];
93 NSLog(@"Can't serialize %@: %@", plist, error);
96 if (![stateData writeToURL:[self urlForStorage] options:NSDataWritingAtomic error:&error]) {
97 NSLog(@"Can't write to %@, error=%@", [self urlForStorage], error);